package org.thirdHero.core.util;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;

/**
 * 反射工具类
 * @author thero
 */
public class ReflectUtils{
 public static Object getFieldValue(Object obj, String fieldName){
     if(obj == null){  
         return null ;  
     }    
     Field targetField = getTargetField(obj.getClass(), fieldName);
       
     try {  
         return FieldUtils.readField(targetField, obj, true ) ;  
     } catch (IllegalAccessException e) {  
         e.printStackTrace();  
     }   
     return null ;
 }
 
 public static Field getTargetField(Class<?> targetClass, String fieldName) {  
     Field field = null;  

     try {  
         if (targetClass == null) {  
             return field;  
         }  

         if (Object.class.equals(targetClass)) {  
             return field;  
         }  

         field = FieldUtils.getDeclaredField(targetClass, fieldName, true);
         if (field == null) {  
             field = getTargetField(targetClass.getSuperclass(), fieldName);  
         }  
     } catch (Exception e) {  
     }  

     return field;  
 }
}